import 'package:flutter/material.dart';

Widget fadeTransitionBuilder(
  BuildContext context,
  Animation<double> animation,
  Animation<double> secondaryAnimation,
  Widget child,
) {
  return FadeTransition(
    opacity: animation,
    child: child,
  );
}

Widget scaleTransitionBuilder(
  BuildContext context,
  Animation<double> animation,
  Animation<double> secondaryAnimation,
  Widget child,
) {
  return ScaleTransition(
    scale: Tween<double>(
      begin: 0.0,
      end: 1.0,
    ).animate(
      CurvedAnimation(
        parent: animation,
        curve: Curves.fastOutSlowIn,
      ),
    ),
    child: child,
  );
}

class AnimationPageRoute extends PageRoute {
  dynamic transitionBuilder;

  AnimationPageRoute({
    @required this.transitionBuilder,
    @required this.builder,
    this.transitionDuration = const Duration(milliseconds: 300),
    this.opaque = true,
    this.barrierDismissible = false,
    this.barrierColor,
    this.barrierLabel,
    this.maintainState = true,
  });

  final WidgetBuilder builder;
  @override
  final Duration transitionDuration;
  @override
  final bool opaque;
  @override
  final bool barrierDismissible;
  @override
  final Color barrierColor;
  @override
  final String barrierLabel;
  @override
  final bool maintainState;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
          Animation<double> secondaryAnimation) =>
      builder(context);

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    return this
        .transitionBuilder(context, animation, secondaryAnimation, child);
  }
}

class FadeAnimationPageRoute extends AnimationPageRoute {
  FadeAnimationPageRoute({
    @required builder,
    transitionDuration = const Duration(milliseconds: 300),
    opaque = true,
    barrierDismissible = false,
    barrierColor,
    barrierLabel,
    maintainState = true,
  }) : super(
          transitionBuilder: fadeTransitionBuilder,
          builder: builder,
          transitionDuration: transitionDuration,
          opaque: opaque,
          barrierDismissible: barrierDismissible,
          barrierColor: barrierColor,
          barrierLabel: barrierLabel,
          maintainState: maintainState,
        );
}

class ScaleAnimationPageRoute extends AnimationPageRoute {
  ScaleAnimationPageRoute({
    @required builder,
    transitionDuration = const Duration(milliseconds: 300),
    opaque = true,
    barrierDismissible = false,
    barrierColor,
    barrierLabel,
    maintainState = true,
  }) : super(
          transitionBuilder: scaleTransitionBuilder,
          builder: builder,
          transitionDuration: transitionDuration,
          opaque: opaque,
          barrierDismissible: barrierDismissible,
          barrierColor: barrierColor,
          barrierLabel: barrierLabel,
          maintainState: maintainState,
        );
}
